const Koa = require('koa')
const app = new Koa()
const Router=require('@koa/router')
const router=new Router()
const {bodyParser}=require('@koa/bodyparser')
const axios=require('axios')
app.use(async (ctx,next)=>{
  ctx.set('Access-Control-Allow-Origin','*')//允许所有域名访问
  ctx.set('Access-Control-Allow-Methods','POST,GET,OPTIONS,PUT,DELETE')//允许的请求方法
  ctx.set('Access-Control-Allow-Headers','Content-Type,Authorization')//允许的请求头
  if(ctx.method==='OPTIONS'){//预检请求
    ctx.status=204
    return
  }
  await next()
})

app.use(bodyParser())//辅助解析post请求体
router.post('/chat',async (ctx)=>{
  const message=ctx.request.body
  console.log(message);
  const data={
    model:'deepseek-r1:7b',
    messages:[message],
    stream:false,//是否流式输出
  }
  const res=await axios.post('http://localhost:11434/api/chat',data)
  console.log(res.data.message.content);
  ctx.body={
    code:200,
    data:{
      message:res.data.message.content
    }
  }
})
app.use(router.routes())
app.listen(3000,()=>{
  console.log('server is running at http://localhost:3000')
})



